package com.idbk.chargestation.activity.pile;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.idbk.chargestation.R;
import com.idbk.chargestation.activity.user.ActivityPointCollection;
import com.idbk.chargestation.adapter.GatherHistoryAdapter;
import com.idbk.chargestation.api.ChargeStationAPI;
import com.idbk.chargestation.base.BaseActivity;
import com.idbk.chargestation.bean.JsonHistory;
import com.idbk.chargestation.bean.JsonHistoryList;
import com.idbk.chargestation.net.EHttpResponse;
import com.idbk.chargestation.util.GsonUtils;
import com.idbk.chargestation.util.UIUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 充电点采集历史
 * @author liangzw,lupc, zhx
 *
 */
public class ActivityPointHistory extends AppCompatActivity implements OnClickListener {

	private Context mContext;
	private ListView mListView;//采集历史现实列表
	private GatherHistoryAdapter mAdapter;//RecyclerView适配器
	private List<JsonHistory> mData;
	private LinearLayout mLayoutEmpty, mLayoutError;//无网络时显示
	private ProgressDialog mDialog;
	private StringRequest mRequest;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_point_history);

		mContext = ActivityPointHistory.this;

		setupView();
	}

	@SuppressWarnings("ConstantConditions")
	private void setupView() {
		mLayoutEmpty = (LinearLayout) findViewById(R.id.layout_empty);
		mLayoutError = (LinearLayout) findViewById(R.id.tj_error_view);
		if (mLayoutError != null) {
			mLayoutError.setOnClickListener(this);
		}

		
		mListView = (ListView) findViewById(R.id.listview);
		mData = new ArrayList<JsonHistory>();
		mAdapter = new GatherHistoryAdapter(mContext, mData);
		mListView.setAdapter(mAdapter);

		findViewById(R.id.toolbar_back).setOnClickListener(this);
		findViewById(R.id.textview_add).setOnClickListener(this);
		findViewById(R.id.textview_new).setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();

		setupData();
	}
	
	private void setupData(){
		mDialog = ProgressDialog.show(mContext, null, 
				"加载中...", true, true, new DialogInterface.OnCancelListener() {
			@Override
			public void onCancel(DialogInterface dialog) {
				cancelCurrentRequest();
				finish();
			}
		});
		mDialog.setCanceledOnTouchOutside(false);

		mRequest = ChargeStationAPI.getMyUploadPoints(mResponse);
	}

	private final EHttpResponse mResponse = new EHttpResponse() {

		@Override
		public void onEResponse(String response) {
			JsonHistoryList result = GsonUtils.toBean(JsonHistoryList.class, response);
			if (BaseActivity.handleResponseStatus(ActivityPointHistory.this, result)) {
				UIUtil.hideAll(mLayoutEmpty, mLayoutError);
				mListView.setVisibility(View.VISIBLE);
				
				mData.clear();
				if(result.otherPiles != null){
					mData.addAll(result.otherPiles);	
				} else {
					UIUtil.showEmpty(mLayoutEmpty, mLayoutError);
					mListView.setVisibility(View.GONE);
				}
				mAdapter.notifyDataSetChanged();
			}
		}

		@Override
		public void onEErrorResponse(VolleyError error) {	
			UIUtil.showNetError(mLayoutEmpty, mLayoutError);
			mListView.setVisibility(View.GONE);
		}

		@Override
		public void onEFinish() {
			// TODO Auto-generated method stub
			super.onEFinish();
			if(mDialog != null){
				mDialog.dismiss();
			}
		}

	};

	private void cancelCurrentRequest(){
		if(mRequest != null){
			mRequest.cancel();
		}
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.toolbar_back:
			finish();
			break;
		case R.id.textview_add:
		case R.id.textview_new:
			Intent intent = new Intent(this, ActivityPointCollection.class);
			startActivity(intent);
			break;
		case R.id.tj_error_view:
			setupData();
			break;
		}
	}

}
